<template>
	<view class="cart-container">
	  <view v-if="cart.length !== 0">
	    <!-- 收货地址区域 -->
	    <view class="address-container">
	      <my-address></my-address>
	    </view>
	  	<!-- 购物车商品列表的标头区域 -->
	    <view class="goods-cart-title">
	      <uni-icons type="shop" size="18"></uni-icons>
	      <text class="text-title">购物车</text>
	    </view>
	    <!-- 左滑删除组件 -->
	    <uni-swipe-action>
	      <block v-for="(item, i) in cart" :key="i">
	        <uni-swipe-action-item :options="options" @click="removeGoods(item)">
	          <!-- 购物的商品列表 -->
	          <my-goods :item="item" :showRadio="true" :showNum="true" @radio-change="radioChangeHandler" @num-change="numberChangeHandler"></my-goods>
	        </uni-swipe-action-item>
	      </block>
	    </uni-swipe-action>
	    
	    <!-- 结算区域 -->
	    <view class="settle">
	      <my-settle></my-settle>
	    </view>
	  </view>
    <view class="empty-cart" v-else>
      <image src="../../static/cart_empty@2x.png" class="empty-img"></image>
      <text>空空如也~</text>
    </view>
  </view>
</template>

<script>
  // 导入自己封装的展示购物车数字徽标的文件
  import bardgeMix from '@/mixins/tabbar-badge.js'
  import { mapState, mapMutations } from 'vuex'
	export default {
    // 将封装的购物车数字徽标的文件在当前页面展示
    mixins: [bardgeMix],
    computed: {
      // 将m_cart模块中的cart商品列表数据展示到当前页
      ...mapState('m_cart', ['cart'])
    },
		data() {
			return {
				options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000'
          }
        }]
			};
		},
    methods: {
      ...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'leftRemoveGoods']),
      // 点击商品的勾选状态和商品的id
      radioChangeHandler (e) {
        this.updateGoodsState(e)
      },
      // 监听数字选择框里的值，发生变化时商品数量也发生变化
      numberChangeHandler (e) {
        this.updateGoodsCount(e)
        console.log(e)
      },
      removeGoods (item) {
        this.leftRemoveGoods(item)
        console.log(item.goods_id)
      }
    }
	}
</script>

<style lang="scss">
.goods-cart-title{
  padding-left: 10px;
  height: 40px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #efefef;
  .text-title{
    font-size: 14px;
    margin-left: 10px;
  }
}
.empty-cart{
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 12px;
  .empty-img{
    width: 90px;
    height: 90px;
    margin-top: 150px;
  }
  text{
    color: gray;
  }
}
.cart-container{
  padding-bottom: 50px;
}
</style>
